package com.health;

import java.util.List;

import com.health.db.DbManager;
import com.health.db.FDbManager;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_ORGANIZE_EMPLOYEE;
import com.health.tablebean.GRAVIDA_BASEINFO;
import com.health.tablebean.HD_PERSONAL_HEALTH_PHOTO;
import com.health.tablebean.JTYSCustomerDoctorCont;
import com.health.tablebean.SBCL_DATA;
import com.health.tools.GlobalConts;
import com.health.tools.IDCardTool;
import com.health.tools.IDCardTool.ReceiveBtStateData;
import com.health.tools.ListDataSave;
import com.health.tools.SPUtil;
import com.novabluetooth.services.ktxyj.Constants;
import com.sdses.BtReaderClient;
import com.tencent.bugly.crashreport.CrashReport;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import static com.health.tools.SPUtil.SP_NAME;
import static com.health.tools.SPUtil.STR_ORIGIN_CODE;

public class NovaApplication extends Application {
    public static DbManager dbManger = null;
    public static FDbManager fdbmanger = null;
    // public static SharedPreferences myPreferences = null;
    public static String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath()
            + GlobalConts.TYPE_DBFILENAME + "/KioskDB.db";
    public static IDCardTool idCardTool;
    private static List<BD_ORGANIZE_EMPLOYEE> doctorList = null;
    public static String updateUrl;// 修改后的地址
    /**
     * 修改后的家庭医生地址
     */
    public static String updateJTYSUrl;
    private static String ftpAddress;// ftp地址
    private static String ftpAccount;// ftp帐号
    private static String ftpPassword;// ftp密码
    private static String db_path;// 数据库地址
    private static String date_tongbu_path;// 数据同步地址
    private static String shangchuan_yanzheng;// 上传验证接口
    private static String shengji_hao;// 产品升级号
    private static String zidong_shengji;// 自动升级

    public static boolean isBluetoothOpen = false;// 默认没有开启
    public static String xuetang;
    public static boolean isXueYa = true;
    private static NovaApplication nova;
    // public static BloothTool bloothTool;
    private static ARCHIVE_BASEINFO userInfo;// 用户基本信息
    public static GRAVIDA_BASEINFO gravida_baseInfo;
    private static ReceiveBtStateData mBtState;
    private static BtReaderClient mClient;
    private static String type;// 国家基本卫生、家庭签约医生

    private static String jgbh = "";

    private static String jgrno = "";
    private static String imei = "";// 设备唯一标识码
    private static String xzqhNo = "";// 行政区号编号

    /**
     * 电子居民健康卡
     */
    public static String str_dzjmjkk_url = "";

    /**
     * 电子居民健康卡 boolean
     */
    private static boolean boolean_dzjmjkk = false;
    static ListDataSave dataSaveDoctorList = null;


    public static String str_jdrusername = "";

    public BtReaderClient getCloientInstance(Activity activity, IDCardTool idcard) {
        if (mClient == null) {
            mClient = new BtReaderClient(activity);
            mBtState = idcard.new ReceiveBtStateData();
            mClient.setCallBack(mBtState);
            return mClient;
        } else {
            return mClient;
        }
    }

    public static ARCHIVE_BASEINFO getUserInfo() {
        if (userInfo == null || userInfo.getIDCARD() == null) {
            String idcard = SPUtil.getstr_idCard(getInstance());
            List<ARCHIVE_BASEINFO> idCardList = fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class,
                    "IDCARD = '" + idcard + "'");
            if (idCardList.size() > 0) {
                userInfo = idCardList.get(0);
            }
        }
        return userInfo;
    }

    public static void setUserInfo(ARCHIVE_BASEINFO userinfo) {
        userInfo = userinfo;
    }

    public static NovaApplication getInstance() {
        return nova;
    }

    ;

    @Override
    public void onCreate() {
        super.onCreate();
        nova = NovaApplication.this;
//        fdbmanger = FDbManager.getInstance(this);
//        dbManger = new DbManager(getApplicationContext());
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
        // 初始化Bugly配置 注册时申请的APPID（a7f2987002）
        CrashReport.initCrashReport(getApplicationContext(), "a7f2987002", true);
        NovaApplication.setType(GlobalConts.TYPE_JTYS);
//        initCSDB();
        dataSaveDoctorList = new ListDataSave(this, SP_NAME);
        GlobalConts.initPath();
        String code = SPUtil.getStr(this, SP_NAME, STR_ORIGIN_CODE);
        Constants.AREA_CODE = TextUtils.isEmpty(code) ? Constants.AREA_CODE : code;
//        initDbData();
    }

    private void initCSDB() {
        SBCL_DATA sb = new SBCL_DATA();// 设备测量
        fdbmanger.save(sb);
        HD_PERSONAL_HEALTH_PHOTO photo = new HD_PERSONAL_HEALTH_PHOTO();// 照片
        fdbmanger.save(photo);
    }

    /**
     * 下载数据库后重新加载一遍dbmanager
     */
    public void instanceFDbManager() {
        // dbManger = new DbManager(getApplicationContext());
        dbManger = null;
        fdbmanger = null;
        dbManger = new DbManager(getApplicationContext());
        fdbmanger = FDbManager.getInstance(this);
    }

    public DbManager getDbManger() {
        return dbManger;
    }

    public void setDbManger(DbManager dbManger) {
        this.dbManger = dbManger;
    }

    public static String getFtpAddress() {
        // if ("".equals(ftpAddress) || ftpAddress == null) {
        ftpAddress = TextUtils.isEmpty(SPUtil.getSysSettings(getInstance()).getFtpAddress()) ? GlobalConts.URL_POST_PORT
                : SPUtil.getSysSettings(getInstance()).getFtpAddress();
        // ftpAddress = myPreferences.getString("ftpAddress",
        // GlobalConts.URL_POST_PORT);
        // }
        return ftpAddress;
    }

    public static void setFtpAddress(String ftpAddress) {
        NovaApplication.ftpAddress = ftpAddress;
    }

    public static String getFtpAccount() {
        // if ("".equals(ftpAccount) || ftpAccount == null) {
        // ftpAccount = myPreferences.getString("ftpAccount", "archivenew");
        ftpAccount = TextUtils.isEmpty(SPUtil.getSysSettings(getInstance()).getFtpAccount()) ? "archivenew"
                : SPUtil.getSysSettings(getInstance()).getFtpAccount();
        // }
        return ftpAccount;
    }

    public static void setFtpAccount(String ftpAccount) {
        NovaApplication.ftpAccount = ftpAccount;
    }

    public static String getFtpPassword() {
        ftpPassword = TextUtils.isEmpty(SPUtil.getSysSettings(getInstance()).getFtpPassword()) ? "archivenew"
                : SPUtil.getSysSettings(getInstance()).getFtpPassword();

        // if ("".equals(ftpPassword) || ftpPassword == null) {
        // ftpPassword = myPreferences.getString("ftpPassword", "archivenew");
        // }
        return ftpPassword;
    }

    public static void setFtpPassword(String ftpPassword) {
        NovaApplication.ftpPassword = ftpPassword;
    }

    public static String getDb_path() {
        db_path = TextUtils.isEmpty(SPUtil.getSysSettings(getInstance()).getDb_path()) ? ""
                : SPUtil.getSysSettings(getInstance()).getDb_path();

        // if ("".equals(db_path) || db_path == null) {
        // db_path = myPreferences.getString("db_path", "");
        // }
        return db_path;
    }

    public static void setDb_path(String db_path) {
        NovaApplication.db_path = db_path;
    }

    public static String getDate_tongbu_path() {
        date_tongbu_path = TextUtils.isEmpty(SPUtil.getSysSettings(getInstance()).getDate_tongbu_path()) ? ""
                : SPUtil.getSysSettings(getInstance()).getDate_tongbu_path();
        //
        // if ("".equals(date_tongbu_path) || date_tongbu_path == null) {
        // date_tongbu_path = myPreferences.getString("date_tongbu_path", "");
        // }
        return date_tongbu_path;
    }

    public static void setDate_tongbu_path(String date_tongbu_path) {
        NovaApplication.date_tongbu_path = date_tongbu_path;
    }

    public static String getShangchuan_yanzheng() {
        shangchuan_yanzheng = TextUtils.isEmpty(SPUtil.getSysSettings(getInstance()).getShangchuan_yanzheng()) ? ""
                : SPUtil.getSysSettings(getInstance()).getShangchuan_yanzheng();

        // if ("".equals(shangchuan_yanzheng) || shangchuan_yanzheng == null) {
        // shangchuan_yanzheng = myPreferences.getString("shangchuan_yanzheng",
        // "");
        // }
        return shangchuan_yanzheng;
    }

    public static void setShangchuan_yanzheng(String shangchuan_yanzheng) {
        NovaApplication.shangchuan_yanzheng = shangchuan_yanzheng;
    }

    public static String getShengji_hao() {
        shengji_hao = TextUtils.isEmpty(SPUtil.getSysSettings(getInstance()).getShengji_hao()) ? ""
                : SPUtil.getSysSettings(getInstance()).getShengji_hao();

        // if ("".equals(shengji_hao) || shengji_hao == null) {
        // shengji_hao = myPreferences.getString("shengji_hao", "");
        // }
        return shengji_hao;
    }

    public static void setShengji_hao(String shengji_hao) {
        NovaApplication.shengji_hao = shengji_hao;
    }

    public static String getZidong_shengji() {
        zidong_shengji = TextUtils.isEmpty(SPUtil.getSysSettings(getInstance()).getZidong_shengji()) ? ""
                : SPUtil.getSysSettings(getInstance()).getZidong_shengji();

        // if ("".equals(zidong_shengji) || zidong_shengji == null) {
        // zidong_shengji = myPreferences.getString("zidong_shengji", "");
        // }
        return zidong_shengji;
    }

    public static void setZidong_shengji(String zidong_shengji) {
        NovaApplication.zidong_shengji = zidong_shengji;
    }

    public static String getType() {
        return type;
    }

    public static void setType(String type) {
        NovaApplication.type = type;
    }

    public static String getJgbh() {
        jgbh = SPUtil.getStrjddwno(getInstance());
        return jgbh;
    }

    public static void setJgbh(String jgbh) {
        NovaApplication.jgbh = jgbh;
    }

    public static String getJgrno() {
        jgrno = SPUtil.getStr_jdrno(getInstance());
        return jgrno;
    }

    public static void setJgrno(String jgrno) {
        NovaApplication.jgrno = jgrno;
    }

    public static List<BD_ORGANIZE_EMPLOYEE> getDoctorList() {
        // if(doctorList==null){
        doctorList = dataSaveDoctorList.getDataList("DoctorList", BD_ORGANIZE_EMPLOYEE.class);
        // }
        return doctorList;
    }

    public static void setDoctorList(List<BD_ORGANIZE_EMPLOYEE> doctorList) {
        NovaApplication.doctorList = doctorList;
        dataSaveDoctorList.setDataList("DoctorList", doctorList);
    }

    public static String getImei() {
        imei = SPUtil.getRegisterImei(getInstance());
        return imei;
    }

    public static void setImei(String imei) {
        NovaApplication.imei = imei;
    }

    public static String getXzqhNo() {
        return SPUtil.getRegisterXZQ(NovaApplication.getInstance());
    }
    public static void setXzqhNo(String xzqhNo) {
        NovaApplication.xzqhNo = xzqhNo;
    }

    public static String getStr_dzjmjkk_url() {
        return SPUtil.getSTR_DZJMJKK_ADDRESS(getInstance());
    }

    public static void setStr_dzjmjkk_url(String str_dzjmjkk_url) {
        NovaApplication.str_dzjmjkk_url = str_dzjmjkk_url;
    }

    public static boolean isBoolean_dzjmjkk() {
        return SPUtil.getBOOLEAN_DZJMJKK(getInstance());
    }

    public static void setBoolean_dzjmjkk(boolean boolean_dzjmjkk) {
        NovaApplication.boolean_dzjmjkk = boolean_dzjmjkk;
    }


    /**
     * 初始化数据表格
     *
     * @author 王婷婷
     * @date 2017年7月26日 下午8:05:23
     */
    private void initDbData() {

        JTYSCustomerDoctorCont jtys_khqyb = new JTYSCustomerDoctorCont();// 家庭签约医生表
        fdbmanger.save(jtys_khqyb);

    }

    public static String getJgrUserLoginName() {
        // return SPUtil.getstr_jdpassward(getInstance());
        return SPUtil.getstr_jdrusername(getInstance());
    }

    public static void setJgrUserLoginName(String jgrno) {
        NovaApplication.str_jdrusername = jgrno;

    }


    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
